/**HEADER********************************************************************
* 
* Copyright (c) 2008 Freescale Semiconductor;
* All Rights Reserved
*
* Copyright (c) 2004-2010 Embedded Access Inc.;
* All Rights Reserved
*
* Copyright (c) 1989-2008 ARC International;
* All Rights Reserved
*
*************************************************************************** 
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************
*
* $FileName: mpc83xx_etsec_join.c$
* $Version : 3.8.1.0$
* $Date    : May-12-2011$
*
* Comments:
*
*            This file contains the Ethernet multicasting
*            support functions.
*
*END************************************************************************/

#include <mqx.h>
#include <bsp.h>

#include <enet.h>
#include <enetprv.h>
#include <mpc83xx_etsec_prv.h>

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_join_MAC
*  Returned Value : void
*  Comments       :
*        Joins a multicast group on an Ethernet channel.
*
*END*-----------------------------------------------------------------*/

uint_32 MPC83xx_etsec_join_MAC
   (
      /* [IN] the Ethernet state structure */
      ENET_CONTEXT_STRUCT_PTR     enet_ptr,

      /* [IN] the multicast control block */
      ENET_MCB_STRUCT_PTR     mcb_ptr
   )
{
   MPC83XX_TSEC_CONTEXT_STRUCT_PTR  tsec_context_ptr;
   MPC83xx_ETSEC_STRUCT_PTR          tsec_ptr;
   uint_32                          crc = 0xFFFFFFFFL;
   uint_32                          i, j;
   uchar                            c;
   uint_32                          error = ENET_OK;

   tsec_context_ptr = enet_ptr->MAC_CONTEXT_PTR;
   tsec_ptr = tsec_context_ptr->TSEC_ADDRESS;


   /* Compute the CRC-32 polynomial on the multicast group */
   for (i = 0; i < 6; i++) {
      c = mcb_ptr->GROUP[i];
      for (j = 0; j < 8; j++) {
         if ((c ^ (crc >> 31)) & 1) {
            crc <<= 1;
            c >>= 1;
            crc ^= 0x04C11DB7L;
         } else {
            crc <<= 1;
            c >>= 1;
         } /* Endif */
      } /* Endfor */
   } /* Endfor */

   mcb_ptr->HASH = (crc >> 24) & 0xFF;

   i = (mcb_ptr->HASH >> 5);
   j = 1 << (mcb_ptr->HASH & 0x1F);

   etsec_lock();

   tsec_ptr->GADDR[i] |= j; 

   etsec_unlock();
   
   return error;

}


/* EOF */
